package com.shujia.interface_;

import org.junit.Test;

public class PersonUtil {

    public static void main(String[] args) {
        PersonUtil util = new PersonUtil();
        Teacher teacher = new Teacher();
        util.paramValTest(teacher);
        // 普通类的子类的匿名对象
        util.paramValTest(new Doctor());

        Person person = new Student();
        util.paramValTest(person);


        Sport sport = new Teacher();
        util.paramValTest(sport);
    }

    @Test
    public void test2() {
        // 抽象类和接口的匿名内部类的使用
        PersonUtil util = new PersonUtil();
        util.paramValTest(new Person() {
            @Override
            void sleep() {
                // 会自动重写抽象方法, 定义代码块它的作用域就仅仅本方法里
                System.out.println("抽象类的匿名内部类的 sleep功能");
            }
        });

        util.paramValTest(new Sport() {
            @Override
            public void run() {
                System.out.println("sport run inner class ...");
            }

            @Override
            public void swimming() {
                System.out.println("sport swimming inner class ...");
            }
        });
    }

    @Test
    public void test1() {
        PersonUtil util = new PersonUtil();
        Teacher teacher = util.returnValTest();

        // ClassCastException
        // Doctor doctor = (Doctor) util.returnValTest();

        Person person = util.returnValTest1();

        Student student = (Student) util.returnValTest1();

        Study study = util.returnValTest2();

        // Teacher teacher1 = (Teacher) util.returnValTest2();

        Doctor doctor1 = (Doctor) util.returnValTest2();
        Teacher teacher1 = doctor1;
    }


    public Teacher returnValTest() {

        Teacher teacher = new Teacher();
        return teacher;

//        return new Doctor();
    }

    public Person returnValTest1() {
        Person person = new Student();
        return person;
    }

    public Study returnValTest2() {
        Study study = new Doctor();
        return study;
    }

    // 普通类
    public void paramValTest(Teacher teacher) {

    }

    // 抽象类
    public void paramValTest(Person person) {
        person.sleep();
    }

    // 接口
    public void paramValTest(Sport sport) {
        sport.run();
        sport.swimming();

    }
}
